package com.woniu.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.Arrays;

/**
 * Json转换配置，集成FastJson
 **/
@Configuration
@ConditionalOnClass({JSON.class})
@ConditionalOnMissingClass
public class JsonHttpConvertersConfigurer {

    @Bean
    public HttpMessageConverters configureMessageConverters() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(
                // 保留map空的字段
                SerializerFeature.WriteMapNullValue,
                // 将String类型的null转成""
                SerializerFeature.WriteNullStringAsEmpty,
                // 将Number类型的null转成0
                SerializerFeature.WriteNullNumberAsZero,
                // 将List类型的null转成[]
                SerializerFeature.WriteNullListAsEmpty,
                // 将Boolean类型的null转成false
                SerializerFeature.WriteNullBooleanAsFalse,
                // 避免循环引用
                SerializerFeature.DisableCircularReferenceDetect,
//                // 跳过 transient 关键字
//                SerializerFeature.SkipTransientField,

                SerializerFeature.WriteEnumUsingToString
        );
        converter.setFastJsonConfig(config);
        converter.setSupportedMediaTypes(
                Arrays.asList(
                        MediaType.APPLICATION_JSON_UTF8,
                        MediaType.APPLICATION_JSON,
                        MediaType.APPLICATION_ATOM_XML,
                        MediaType.APPLICATION_FORM_URLENCODED,
                        MediaType.APPLICATION_OCTET_STREAM,
                        MediaType.APPLICATION_PDF,
                        MediaType.APPLICATION_RSS_XML,
                        MediaType.APPLICATION_XHTML_XML,
                        MediaType.APPLICATION_XML,
                        MediaType.IMAGE_GIF,
                        MediaType.IMAGE_JPEG,
                        MediaType.IMAGE_PNG,
                        MediaType.TEXT_EVENT_STREAM,
                        MediaType.TEXT_HTML,
                        MediaType.TEXT_MARKDOWN,
                        MediaType.TEXT_PLAIN,
                        MediaType.TEXT_XML
                )
        );
        HttpMessageConverter<?> converters = converter;
        return new HttpMessageConverters(converters);
    }

}
